package cn.maidouya.hrm.handler;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *  授权失败回调页面实现AccessDeniedHandler
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    // 重写handle方法
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        Map map = new HashMap<>();
        map.put("success",false);
        map.put("message","授权失败");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().print(JSON.toJSONString(map));
        // 字符流，避免数据在缓冲区，使用flush发布
        response.getWriter().flush();
        response.getWriter().close();
    }
}
